package com.jymj.service.impl;

import com.jymj.dao.SysXzqRoleDao;
import com.jymj.entity.SysXzqEntity;
import com.jymj.service.SysUserRoleService;
import com.jymj.service.SysXzqRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by ljj on 2017/6/1.
 */
@Service
public class SysXzqRoleServiceImpl implements SysXzqRoleService {
    @Autowired
    private SysXzqRoleDao sysXzqRoleDao;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public List<Long> queryXzqIdList(Long roleId) {
        return sysXzqRoleDao.queryXzqIdList(roleId);
    }

    @Override
    public void saveOrUpdate(Long roleId, List<Long> xzqIdList) {
        if(xzqIdList.size() == 0){
            return ;
        }
        //先删除角色与菜单关系
        sysXzqRoleDao.delete(roleId);

        //保存角色与菜单关系
        Map<String, Object> map = new HashMap<>();
        map.put("roleId", roleId);
        map.put("xzqIdList", xzqIdList);
        sysXzqRoleDao.save(map);
    }

    @Override
    public List<SysXzqEntity> queryXzqList(Long userId) {
        return sysXzqRoleDao.queryXzqList(userId);
    }

    @Override
    public List<SysXzqEntity> getUserTownList(Long userId) {
        return sysXzqRoleDao.getUserTownList(userId);
    }

    @Override
    public List<SysXzqEntity> getUserDistrictList(Long userId) {
        return sysXzqRoleDao.getUserDistrictList(userId);
    }

    @Override
    public List<SysXzqEntity> getXzqInfoList(Long userId) {
        return sysXzqRoleDao.getXzqInfoList(userId);
    }

    @Override
    public void save(Long xzqId, Long userId) {
        //先查询用户的权限
        List<Long> list = sysUserRoleService.queryRoleIdList(userId);
        //判断角色与行政区是否存在
        List<Long> list1 = sysXzqRoleDao.queryXzqIdList(list.get(0));
        if (Arrays.asList(list1).contains(xzqId)){
            return;
        }
        //添加行政区与角色关系
        Map<String, Object> map = new HashMap<>();
        map.put("roleId", list.get(0));
        List<Long> xzqIdList = new ArrayList<>();
        xzqIdList.add(xzqId);
        map.put("xzqIdList", xzqIdList);
        sysXzqRoleDao.save(map);
    }

    @Override
    public List<String> qeryCode(Long userId) {
        return sysXzqRoleDao.qeryCode(userId);
    }
}
